package tt.com.business.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import tt.com.business.mapper.DibaoMapper;
import tt.com.business.service.DibaoService;
import tt.com.common.model.Dibao;
import tt.com.common.util.PageUtil;

import java.util.List;

@Service
public class Dibaolmpl extends ServiceImpl<DibaoMapper, Dibao> implements DibaoService {
    @Override
    public ResponseEntity selectDibao(PageUtil pageUtil) {

        IPage<Dibao> page = new Page<>(pageUtil.getPageIndex(), pageUtil.getPageSize());
        QueryWrapper<Dibao> qw=new QueryWrapper<>(null);
        IPage<Dibao> page1 = baseMapper.selectPage(page, qw);
        pageUtil.setData(page1);
        //设计总数
        pageUtil.setTotalSize(baseMapper.selectCount(null));
        //设置总页数
        pageUtil.setTotalPage(pageUtil.getPageSize()/pageUtil.getPageSize());
        return new ResponseEntity(pageUtil, HttpStatus.OK);

    }

    @Override
    public ResponseEntity addDIbao(Dibao dibao) {
        String idCard = dibao.getIdCard();
        //判断是否有重复的低保人员信息
        QueryWrapper<Dibao> wrapper = new QueryWrapper<>();
        wrapper.eq("id_card",idCard);
        List<Dibao> dibaos = baseMapper.selectList(wrapper);
        if (dibaos.size()>0){
            return new ResponseEntity("该人信息也填写",HttpStatus.INTERNAL_SERVER_ERROR);
        }

        //查询低保用户总数
        Integer integer = baseMapper.selectCount(null);
        //设置id
        dibao.setId(String.valueOf(integer+1));
        //设置为确定减免
        dibao.setStatus(1);
        //添加信息
        int insert = baseMapper.insert(dibao);
        if (insert<0){
            return new ResponseEntity("添加失败",HttpStatus.INTERNAL_SERVER_ERROR);
        }
        return new ResponseEntity("该人信息添加成功",HttpStatus.OK);
    }
}
